package com.xinqi.modules.chat.chat.official.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.official.domain.ChatAssistantConfigEntity;
import com.xinqi.modules.chat.chat.official.manager.ChatAssistantConfigManager;
import com.xinqi.modules.chat.chat.official.mapper.ChatAssistantConfigMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 新奇助手通知设置表管理层实现
 *
 * @author: yyzhen
 * @date: 2023/05/05
 */
@Service
@RequiredArgsConstructor
public class ChatAssistantConfigManagerImpl extends BaseServiceImpl<ChatAssistantConfigMapper, ChatAssistantConfigEntity> implements ChatAssistantConfigManager {


    /**
     * 根据用户获取新奇助手通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public ChatAssistantConfigEntity selectByUserId(Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(userId), MessageUtils.message("user.id.not.black"));
        return this.baseMapper.selectOne(wrapper(userId));
    }

    /**
     * 根据用户集合获取新奇助手通知设置
     * @param userIds
     * @return
     */
    @Override
    public List<ChatAssistantConfigEntity> selectByUserIds(List<Long> userIds){
        LambdaQueryWrapper<ChatAssistantConfigEntity> wrapper = new LambdaQueryWrapper();
        wrapper.in(ChatAssistantConfigEntity::getUserId, userIds);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 初始化用户新奇助手通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public boolean initByUserId(Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(userId), MessageUtils.message("user.id.not.black"));
        ChatAssistantConfigEntity configEntity = this.baseMapper.selectOne(wrapper(userId));
        if (Objects.isNull(configEntity)) {
            //没有则初始化
            ChatAssistantConfigEntity init = new ChatAssistantConfigEntity();
            init.setUserId(userId);
            init.setIsDisturb(YesNoNumberEnum.NO.getCode());
            init.setTask(YesNoNumberEnum.YES.getCode());
            init.setTest(YesNoNumberEnum.YES.getCode());
            init.setTopic(YesNoNumberEnum.YES.getCode());
            init.setNotice(YesNoNumberEnum.YES.getCode());
            init.setAttendance(YesNoNumberEnum.YES.getCode());
            init.setResource(YesNoNumberEnum.YES.getCode());
            init.setInteraction(YesNoNumberEnum.YES.getCode());
            init.setCourseware(YesNoNumberEnum.YES.getCode());
            init.setVideo(YesNoNumberEnum.YES.getCode());

            this.baseMapper.insert(init);
        }
         return Boolean.TRUE;
    }

    private LambdaQueryWrapper<ChatAssistantConfigEntity> wrapper(Long userId) {
        LambdaQueryWrapper<ChatAssistantConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatAssistantConfigEntity::getUserId, userId)
            .last("LIMIT 1");
        return wrapper;
    }

}
